package com.zp.qualitypractice.list;

import android.support.annotation.IdRes;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Created by izp on 2017/4/18.
 */

public class BaseViewHolder extends RecyclerView.ViewHolder {
    private SparseArray<View> mViewSparseArray;
    public BaseViewHolder(View itemView) {
        super(itemView);
        mViewSparseArray = new SparseArray<>();
    }

    private <T extends View> T getViewById(@IdRes int viewId){
        View view = mViewSparseArray.get(viewId);
        if (null==view){
            view =itemView.findViewById(viewId);
            mViewSparseArray.put(viewId, view);
        }
        return (T) view;
    }
    public View getView(int viewId) {
        return getViewById(viewId);
    }

    public TextView getTextView(int viewId) {
        return (TextView) getView(viewId);
    }

    public Button getButton(int viewId) {
        return (Button) getView(viewId);
    }

    public ImageView getImageView(int viewId) {
        return (ImageView) getView(viewId);
    }

    public BaseViewHolder setText(int viewId, String value) {
        TextView view = getViewById(viewId);
        view.setText(value);
        return this;
    }

}
